package com.itheima.mp.controller;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;
//@AllArgsConstructor,lombok的注解,帮助生成构造函数,这个是构造全部
@RequiredArgsConstructor//必备属性会构造其他不用
@Api(tags = "用户管理接口")
@RequestMapping("/users")
@RestController
public class UserController {
    //spring不提倡自动注入，建议构造注入，但是太繁琐所以用@RequiredArgsConstructor

    private final IUserService userService;


    @ApiOperation("新增用户接口")
    @PostMapping
    public void saveUser(@RequestBody UserFormDTO userDTO){
        //1.拷贝
        User user= BeanUtil.copyProperties(userDTO,User.class);
        //2.新增
        userService.save(user);
    }




   @ApiOperation("删除用户接口")
   @DeleteMapping("/{id}")
   public void deleteUserById(@ApiParam("用户id") @PathVariable("id") Long id){

       userService.removeById(id);
   }




    @ApiOperation("根据id查询用户接口")
    @GetMapping("{id}")
    public UserVO queryUserById(@ApiParam("用户id") @PathVariable("id") Long id){
        //1.查询
        //User user = userService.getById(id);
        //2.把PO拷贝到VO
        //return BeanUtil.copyProperties(user,UserVO.class);
        return userService.queryUserAndAddressById(id);

    }



    @ApiOperation("根据id批量查询用户接口")
    @GetMapping
    public List<UserVO> queryUserByIds(@ApiParam("用户id集合") @RequestParam("ids") List<Long> ids){
        //1.查询
        //List<User> users = userService.listByIds(ids);
        //2.把PO拷贝到VO
        //return BeanUtil.copyToList(users,UserVO.class);
        //直接拷贝集合
        return userService.queryUserAndAddressByIds(ids);
    }


    @ApiOperation("扣减用户余额接口")
    @PutMapping("/{id}/deduction/{money}")
    public void deductBalance(
            @ApiParam("用户id") @PathVariable("id") Long id,
            @ApiParam("扣减的金额") @PathVariable("money") Integer money) {
        userService.deductBalance(id,money);
    }

    @GetMapping("/list")
    @ApiOperation("根据id集合查询用户")
    public List<UserVO> queryUsers(UserQuery query){
        // 1.组织条件
        String username = query.getName();
        Integer status = query.getStatus();
        Integer minBalance = query.getMinBalance();
        Integer maxBalance = query.getMaxBalance();
        // 2.查询用户
        List<User> users = userService.lambdaQuery()
                .like(username != null, User::getUsername, username)
                .eq(status != null, User::getStatus, status)
                .ge(minBalance != null, User::getBalance, minBalance)
                .le(maxBalance != null, User::getBalance, maxBalance)
                .list();
        // 3.处理vo
        return BeanUtil.copyToList(users, UserVO.class);
    }


    @GetMapping("/page")
    @ApiOperation("根据条件分页查询")
    public PageDTO<UserVO> queryUsersPage(UserQuery query){
        //查询参数还是userquery，现在继承了分页PageQuery范围更广
        //返回结果不是list<uservo>,变成pagedto
        return userService.queryUsersPage(query);
    }

}
